package com.utaedang.jesseclub;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.media.Ringtone;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.RingtonePreference;
import android.util.Log;
import android.view.Window;

public class Prefs extends PreferenceActivity implements Preference.OnPreferenceChangeListener{
	private static final String TAG = Prefs.class.getSimpleName();
	
	private static final String AUTO_DISPLAY_OFF = "DisplayOff";
	private static final boolean AUTO_DISPLAY_OFF_VALUE = true;
	private static final String AUTO_REFRESH_SEC = "RefreshSecond";
	private static final String AUTO_REFRESH_SEC_VALUE = "20";
		
	SharedPreferences mainPreference;
	PreferenceCategory jesseClub;
	ListPreference RefreshSecond;
	CheckBoxPreference DisplayOff;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//Title 변경 방법
        this.setTitle("환경설정");		
        addPreferencesFromResource(R.xml.settings);
								
		mainPreference = PreferenceManager.getDefaultSharedPreferences(this); 
		//getPreferenceManager().setSharedPreferencesName("mbt");
		
		jesseClub = (PreferenceCategory)findPreference("jesseclub");
		
		DisplayOff = (CheckBoxPreference)findPreference("DisplayOff");
		DisplayOff.setOnPreferenceChangeListener(this);
		DisplayOff.setChecked(mainPreference.getBoolean("DisplayOff", false));
				
		RefreshSecond = (ListPreference)findPreference("RefreshSecond");
		RefreshSecond.setOnPreferenceChangeListener(this);
		if(RefreshSecond.getValue().equals("NO")){
			RefreshSecond.setSummary("설정 안함");
		}else{
			RefreshSecond.setSummary(RefreshSecond.getValue()+"초");
		}
	}
	
	public static String getAutoRefresh(Context context){
		return PreferenceManager.getDefaultSharedPreferences(context).getString(AUTO_REFRESH_SEC, AUTO_REFRESH_SEC_VALUE);
	}
	
	public static boolean getDisplayOff(Context context){
		return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(AUTO_DISPLAY_OFF, AUTO_DISPLAY_OFF_VALUE);
	}
	
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		Log.d(TAG, preference.getKey()+"="+ newValue);
		if (preference.getKey().equals("RefreshSecond")) {
			if(!newValue.equals("NO")){
				preference.setSummary((String)newValue + "초");
			}else{
				preference.setSummary("설정 안함");
			}
		}
		if (preference.getKey().equals("DisplayOff")) {
			
		}
		if (preference.getKey().equals("Ringtone")) {
			//updateRingtonePreferenceSummary((String) newValue);
		}
		return true;
	}
	/*
	private void updateRingtonePreferenceSummary(String ringtoneUrl) {

		if (ringtoneUrl != null && ringtoneUrl.trim().equals("")) {
			Ringtone.setSummary("Silent");
			return;
		}

		Uri ringtoneUri = Uri.parse(ringtoneUrl);
		Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);

		String title = null;
		if (ringtone != null) {
			title = ringtone.getTitle(this);
		}

		Ringtone.setSummary(title);
	}
	*/
}
